#include <iostream>
#include <stdlib.h>
#include "mutex.hpp"
#include <unistd.h>

using namespace std;
using namespace LockModule;

// void * route(void* arg)
// {
//     LockGuard lg();
//     char* name = (char*)arg;
//     cout<< name <<": is working"<<endl;
// }

int ticket = 1000;
Mutex mutex;

void *getticket(void *arg)
{
    char *name = static_cast<char *>(arg);
    while(1)
    {
        usleep(10000);
        LockGuard lockguard(mutex);
        if (ticket == 0)
        {
            cout << "ticket run out!" << endl;
            exit(1);
        }
        cout << name << ": get a ticket,leave tickets:" << --ticket << endl;
    }
    return NULL;
}

int main()
{

    // pthread_t t1,t2,t3;
    // pthread_create(&t1,NULL,route,(void*)"thread 1");
    // pthread_create(&t2,NULL,route,(void*)"thread 2");
    // pthread_create(&t3,NULL,route,(void*)"thread 3");

    // pthread_join(t1,NULL);
    // pthread_join(t2,NULL);
    // pthread_join(t3,NULL);
  

    pthread_t t1, t2, t3;
    pthread_create(&t1, NULL, getticket, (void *)"thread 1");
    pthread_create(&t2, NULL, getticket, (void *)"thread 2");
    pthread_create(&t3, NULL, getticket, (void *)"thread 3");

    pthread_join(t1, NULL);
    pthread_join(t2, NULL);
    pthread_join(t3, NULL);

    return 0;
}